package com.fanfan.SmartPensionSystembackend.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fanfan.SmartPensionSystembackend.entity.Bed;
import com.fanfan.SmartPensionSystembackend.mapper.BedMapper;
import com.fanfan.SmartPensionSystembackend.service.BedService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class BedServiceImpl extends ServiceImpl<BedMapper, Bed> implements BedService {

    // 批量插入床位
    @Override
    @Transactional
    public boolean addBeds(List<Bed> beds) {
        // 获取当前最大ID
        Integer maxId = getBaseMapper().selectMaxId();
        if (maxId == null) {
            maxId = 0;
        }

        // 设置bedId
        final int startId = maxId + 1;
        for (int i = 0; i < beds.size(); i++) {
            Bed bed = beds.get(i);
            bed.setBedId((String.format("%04d", startId + i)));
            bed.setStatus("空"); // 默认状态
            bed.setGender(null); // 默认null
        }

        // 添加调试日志
        beds.forEach(bed -> {
            System.out.println("Inserting bed: " + bed);
        });

        // 显式调用insertBed方法进行插入
        beds.forEach(bed -> getBaseMapper().insertBed(bed));

        return true;
    }

    // 获取所有床位信息
    @Override
    public List<Bed> getAllBedList() {
        return getBaseMapper().getAllBedList();
    }

    // 删除床位
    @Override
    public boolean deleteBed(String bedId) {
        return getBaseMapper().deleteById(bedId) > 0;
    }

    // 筛选床位
    @Override
    public List<Bed> screenBeds(String buildingNo, String floorNo, String roomNo) {
        return getBaseMapper().screenBeds(buildingNo, floorNo, roomNo);
    }

}